if exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[vw_purchases_orders_not_labeled_distributions_available]') AND OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_purchases_orders_not_labeled_distributions_available]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

/***********************************************************************
* vw Name: dbo.vw_purchases_orders_not_labeled_distributions_available
* Created: 20101118, RGM
* Purpose: listado de las cantidades disponibles de lo no etiquetado y
*	no distribuido por PO
*
* Modified: yyyymmdd, user, data
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
CREATE VIEW dbo.vw_purchases_orders_not_labeled_distributions_available AS
SELECT pov.purchase_order_version_id, pov.month_period_version_id, pov.qty_not_labeled, 
		SUM(ISNULL(mdpd.quantity, 0)) qty_distributed, 
		pov.qty_not_labeled - SUM(ISNULL(mdpd.quantity, 0)) qty_available
	FROM vw_purchases_orders_distributions_amounts pov
		LEFT OUTER JOIN monthly_distributions_per_plants_details mdpd
			ON pov.purchase_order_version_id = mdpd.purchase_order_version_id
			AND mdpd.labeled = 0
	GROUP BY pov.purchase_order_version_id, pov.month_period_version_id, pov.qty_not_labeled

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

